package com.javaweb.blogcite.controller;

import com.javaweb.blogcite.aspect.JwtCheck;
import com.javaweb.blogcite.common.ResponseResult;
import com.javaweb.blogcite.controller.vo.BlogTypeView;
import com.javaweb.blogcite.service.BlogTypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class BlogTypeController {
    @Resource
    private BlogTypeService blogtypeService;

    @GetMapping("/api/blogtype/{id}")
    public ResponseResult<BlogTypeView> getBlogTypeById(@PathVariable(value="id") Integer id) {
        BlogTypeView blogTypeView = blogtypeService.getBlogTypeById(id);
        ResponseResult<BlogTypeView> responseResult = new ResponseResult<>();
        responseResult.setCode(0);
        responseResult.setMsg("success");
        responseResult.setData(blogTypeView);

        return responseResult;
    }

    @JwtCheck
    @DeleteMapping("/api/blogtype/{id}")
    public ResponseResult deleteBlogType(@PathVariable("id") Integer id) {
        boolean success = blogtypeService.deleteBlogTypeById(id);
        ResponseResult result = new ResponseResult();
        if (success) {
            result.setCode(0);
            result.setMsg("博客分类删除成功！");
        } else {
            result.setCode(500);
            result.setMsg("无！");
        }
        return result;
    }


    @GetMapping("/api/blogtype")
    public ResponseResult<List<BlogTypeView>> getBlogTypeByOrder() {
        List<BlogTypeView> blogTypeView = blogtypeService.getBlogTypeByOrder();
        ResponseResult<List<BlogTypeView>> responseResult = new ResponseResult<>();
        responseResult.setCode(0);
        responseResult.setMsg("success");
        responseResult.setData(blogTypeView);
        return responseResult;
    }

    @JwtCheck
    @PostMapping("/api/blogtype")
    public ResponseResult postBlogType(@RequestBody BlogTypeView requestBody) {
        boolean success = blogtypeService.addBlogType(requestBody);
        ResponseResult result = new ResponseResult();
        if (success) {
            result.setCode(0);
            result.setMsg("博客分类添加成功！");
        } else {
            result.setCode(500);
            result.setMsg("博客分类添加失败！");
        }
        return result;
    }

    @JwtCheck
    @PutMapping("/api/blogtype/{id}")
    public ResponseResult putBlogType(@PathVariable Integer id, @RequestBody BlogTypeView requestBody) {
        boolean success = blogtypeService.updateBlogType(requestBody);
        ResponseResult result = new ResponseResult();
        if (success) {
            result.setCode(0);
            result.setMsg("博客分类更新成功！");
        } else {
            result.setCode(500);
            result.setMsg("博客分类更新失败！");
        }
        return result;
    }
}
